package com.example.playnote

import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.widget.TextView
import androidx.appcompat.app.ActionBarDrawerToggle
import androidx.appcompat.widget.AppCompatButton
import androidx.core.view.GravityCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction
import com.example.playnote.activity.login.LoginActivity
import com.example.playnote.activity.ui.AboutActivity
import com.example.playnote.activity.ui.SearchActivity
import com.example.playnote.base.BaseActivity
import com.example.playnote.bean.Constant
import com.example.playnote.bean.ToastFlag
import com.example.playnote.databinding.ActivityMainBinding
import com.example.playnote.fragment.CommonUseFragment
import com.example.playnote.fragment.HomeFragment
import com.example.playnote.fragment.TypeFragment
import com.example.playnote.utils.MMKVUtils
import com.example.playnote.viewModel.LoginViewModel
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.google.android.material.navigation.NavigationView

class MainActivity : BaseActivity<LoginViewModel, ActivityMainBinding>() {
    private var lastTime: Long = 0
    private var currentIndex = 0
    private var homeFragment: HomeFragment? = null
    private var typeFragment: TypeFragment? = null
    private var commonUseFragment: CommonUseFragment? = null
    private val fragmentManager by lazy {
        supportFragmentManager
    }

    /**
     * check login for SharedPreferences
     */
    private val isLogin: Boolean by lazy {
        MMKVUtils.getBoolean(Constant.LOGIN_KEY) ?: false
    }
    /**
     * local username
     */
    private val username: String by lazy {
        MMKVUtils.getString(Constant.USERNAME_KEY) ?: ""
    }

    /**
     * login username
     */
    private lateinit var navigationViewUsername: TextView

    /**
     * login or logout
     */
    private lateinit var navigationViewLogout: AppCompatButton

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.toolbar.run {
            title = getString(R.string.app_name)
            setSupportActionBar(this)
        }
        mDatabind.bottomNavigation.run {
            setOnNavigationItemSelectedListener(onNavigationItemSelectedListener)
            selectedItemId = R.id.navigation_home
        }
        // 抽屉设置 开启关闭在toolBar中有不同图标
        mDatabind.drawerLayout.run {
            val toggle = ActionBarDrawerToggle(
                this@MainActivity,
                this,
                mDatabind.toolbar,
                R.string.navigation_drawer_open,
                R.string.navigation_drawer_close
            )
            addDrawerListener(toggle)
            toggle.syncState()
        }
        mDatabind.navigationView.run {
            setNavigationItemSelectedListener(onDrawerNavigationItemSelectedListener)
        }
        navigationViewUsername =
            mDatabind.navigationView.getHeaderView(0).findViewById<TextView>(R.id.navigationViewUsername)
        navigationViewLogout = mDatabind.navigationView.getHeaderView(0)
            .findViewById<AppCompatButton>(R.id.navigationViewLogout)
        navigationViewUsername.run {
            text = if (!isLogin) {
                getString(R.string.not_login)
            } else {
                username
            }
        }
        navigationViewLogout.run {
            text = if (!isLogin) {
                getString(R.string.goto_login)
            } else {
                getString(R.string.logout)
            }
            setOnClickListener {
                if (!isLogin) {
                    Intent(this@MainActivity, LoginActivity::class.java).run {
                        startActivityForResult(this, Constant.MAIN_REQUEST_CODE)
                    }
                } else {
                    MMKVUtils.clear()
                    navigationViewUsername.text = getString(R.string.not_login)
                    text = getString(R.string.goto_login)
                    homeFragment?.refreshData()
                }
            }
        }
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        menuInflater.inflate(R.menu.menu_main, menu)
        return super.onCreateOptionsMenu(menu)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.menuSearch -> {
                Intent(this, SearchActivity::class.java).run {
                    startActivity(this)
                }
                return true
            }
            R.id.menuHot -> {
                if (currentIndex == R.id.menuHot) {
                    commonUseFragment?.refreshData()
                }
                setFragment(R.id.menuHot)
                currentIndex = R.id.menuHot
                return true
            }
        }
        return super.onOptionsItemSelected(item)
    }

    /**
     * Dispatch incoming result to the correct fragment.
     */
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when (requestCode) {
            Constant.MAIN_REQUEST_CODE -> {
                if (resultCode == RESULT_OK) {
                    navigationViewUsername.text = data?.getStringExtra(Constant.CONTENT_TITLE_KEY)
                    navigationViewLogout.text = getString(R.string.logout)
                }
                homeFragment?.refreshData()
            }
            Constant.MAIN_LIKE_REQUEST_CODE -> homeFragment?.refreshData()
        }
    }

    /**
     * 防止重叠
     */
    override fun onAttachFragment(fragment: Fragment) {
        super.onAttachFragment(fragment)
        when (fragment) {
            is HomeFragment -> homeFragment ?: let { homeFragment = fragment }
            is TypeFragment -> typeFragment ?: let { typeFragment = fragment }
            is CommonUseFragment -> commonUseFragment ?: let { commonUseFragment = fragment }
        }
    }

    /**
     * 退出
     */
    override fun onBackPressed() {
        if (mDatabind.drawerLayout.isDrawerOpen(GravityCompat.START)) {
            mDatabind.drawerLayout.closeDrawer(GravityCompat.START)
            return
        }
        val currentTime = System.currentTimeMillis()
        if (currentTime - lastTime < 2 * 1000) {
            super.onBackPressed()
            finish()
        } else {
            this.toasty(ToastFlag.NORMAL,  getString(R.string.double_click_exit))
            lastTime = currentTime
        }
    }

    override fun onResume() {
        super.onResume()
        // other activity login
        if (isLogin && navigationViewUsername.text.toString() != username) {
            navigationViewUsername.text = username
            navigationViewLogout.text = getString(R.string.logout)
            homeFragment?.refreshData()
        }
    }

    override fun initImmersionBar() {
        super.initImmersionBar()
        immersionBar.titleBar(mDatabind.toolbar).init()
    }

    /**
     * NavigationItemSelect监听
     */
    private val onNavigationItemSelectedListener =
        BottomNavigationView.OnNavigationItemSelectedListener { item ->
            setFragment(item.itemId)
            return@OnNavigationItemSelectedListener when (item.itemId) {
                R.id.navigation_home -> {
                    if (currentIndex == R.id.navigation_home) {
                        homeFragment?.smoothScrollToPosition()
                    }
                    currentIndex = R.id.navigation_home
                    true
                }
                R.id.navigation_type -> {
                    if (currentIndex == R.id.navigation_type) {
                        typeFragment?.smoothScrollToPosition()
                    }
                    currentIndex = R.id.navigation_type
                    true
                }
                else -> {
                    false
                }
            }
        }

    private val onDrawerNavigationItemSelectedListener =
        NavigationView.OnNavigationItemSelectedListener { item ->
            when (item.itemId) {
                R.id.nav_like -> {
                    if (!isLogin) {
                        this.toasty(ToastFlag.ERROR, getString(R.string.login_please_login))
                        Intent(this, LoginActivity::class.java).run {
                            startActivityForResult(this, Constant.MAIN_REQUEST_CODE)
                        }
                        return@OnNavigationItemSelectedListener true
                    }
                    Intent(this, SearchActivity::class.java).run {
                        putExtra(Constant.SEARCH_KEY, false)
                        startActivityForResult(this, Constant.MAIN_LIKE_REQUEST_CODE)
                    }
                }
                R.id.nav_about -> {
                    Intent(this, AboutActivity::class.java).run {
                        startActivity(this)
                    }
                }
            }
            mDatabind.drawerLayout.closeDrawer(GravityCompat.START)
            true
        }
    /**
     * 显示对应Fragment
     */
    private fun setFragment(index: Int) {
        if (mDatabind.drawerLayout.isDrawerOpen(GravityCompat.START)) {
            mDatabind.drawerLayout.closeDrawer(GravityCompat.START)
        }
        fragmentManager.beginTransaction().apply {
            homeFragment ?: let {
                HomeFragment().let {
                    homeFragment = it
                    add(R.id.content, it)
                }
            }
            typeFragment ?: let {
                TypeFragment().let {
                    typeFragment = it
                    add(R.id.content, it)
                }
            }
            commonUseFragment ?: let {
                CommonUseFragment().let {
                    commonUseFragment = it
                    add(R.id.content, it)
                }
            }
            hideFragment(this)
            when (index) {
                R.id.navigation_home -> {
                    mDatabind.toolbar.title = getString(R.string.app_name)
                    homeFragment?.let {
                        this.show(it)
                    }
                }
                R.id.navigation_type -> {
                    mDatabind.toolbar.title = getString(R.string.title_dashboard)
                    typeFragment?.let {
                        this.show(it)
                    }
                }
                R.id.menuHot -> {
                    mDatabind.toolbar.title = getString(R.string.hot_title)
                    commonUseFragment?.let {
                        this.show(it)
                    }
                }
            }
        }.commit()
    }

    /**
     * 隐藏所有fragment
     */
    private fun hideFragment(transaction: FragmentTransaction) {
        homeFragment?.let {
            transaction.hide(it)
        }
        typeFragment?.let {
            transaction.hide(it)
        }
        commonUseFragment?.let {
            transaction.hide(it)
        }
    }
}